﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class PhotonNetworkManager : Photon.MonoBehaviour
 {


	private RoomOptions options;
	[HideInInspector]public GameObject player;
	
	
	public Text networkStatusText;
	public Button spawnButton;
	public Button findGameButton;
	public int myTeam;
	[HideInInspector] public SpawnManager spawnManager;
	public float timeToLoad;//time delay to account for setting up gamestate

	void Awake()
	{	
		Connect ();
	}

	void Update()
	{	
		if (networkStatusText.enabled == true)
		networkStatusText.text = PhotonNetwork.connectionStateDetailed.ToString ();
	}
		

	public void Connect ()
	{		

		options = new RoomOptions()
		{
			MaxPlayers = 10,
		};

		if (PhotonNetwork.connectionStateDetailed.ToString () != "JoinedLobby")
		{
			PhotonNetwork.ConnectUsingSettings (GameConstants.Version);
		}

	}
		

	public virtual void OnJoinedLobby()
	{
		findGameButton.interactable = true;
	}
		
	public void GetRooms()
	{		
		foreach (RoomInfo room in PhotonNetwork.GetRoomList()) 
		{
			Debug.Log("there is a room");
			Debug.Log (room.Name);
			Debug.Log (room.PlayerCount);
			Debug.Log (room.MaxPlayers);
		}
	}

	public void JoinBestRoom()
	{	
		findGameButton.interactable = false;
		int mostPlayers = 0;
		string bestRoomName;
		bestRoomName = null;
		foreach (RoomInfo room in PhotonNetwork.GetRoomList())
		{
			if ((room.PlayerCount > mostPlayers) && (room.PlayerCount < room.MaxPlayers))
			{
				mostPlayers = room.PlayerCount;
				bestRoomName = room.Name;

			}

		}

		if (bestRoomName != null)
		{
			PhotonNetwork.JoinRoom (bestRoomName);

		} else
		{
			PhotonNetwork.CreateRoom ("", options, null);
		}
	
		
	}


	public void DebugBestRoom()
	{
		int mostPlayers = 0;
		string bestRoomName;
		bestRoomName = null;
		foreach (RoomInfo room in PhotonNetwork.GetRoomList())
		{	
			Debug.Log (room.Name);
			if ((room.PlayerCount > mostPlayers) && (room.PlayerCount < room.MaxPlayers))
			{
				mostPlayers = room.PlayerCount;
				bestRoomName = room.Name;

			}

		}
		Debug.Log (bestRoomName);
	}
	public virtual void OnJoinedRoom()
	{	
		Debug.Log (PhotonNetwork.room.Name);
		StartCoroutine(WaitToLoad());
		
	}
	IEnumerator WaitToLoad()
	{
		yield return new WaitForSeconds(timeToLoad);
		GameManager.instance.uiManager.Limbo ();
		spawnButton.interactable = true;
	}
	void SetTeam()
	{	
		int[] count = new int[2];
		count [0] = 0;
		count [1] = 0;
		TeamAble[] players = GameObject.FindObjectsOfType<TeamAble> ();
		foreach (TeamAble player in players)
		{
			if (player.isPlayer && player.team < 2 && player != this)
			{
				count [player.team]++;
			}
		}
		Debug.Log("we found " + count[0].ToString() + " players in team 0");
		Debug.Log ("we found " + count [1].ToString () + " players in team 1");
		if (count [0] > count [1])
		{
			myTeam = 1;
		} else
		{
			myTeam = 0;
		}
		Debug.Log ("we will join team " + myTeam.ToString ());
		spawnManager.spawnPoint = spawnManager.spawnPoints [myTeam];
		SetAllColors ();
	}

	public void SetAllColors()
	{
		TeamAble[] players = GameObject.FindObjectsOfType<TeamAble> ();
		foreach (TeamAble player in players)
		{
			player.SetColor ();
		}
	}
		
	public void Spawn()
	{	
		SetTeam ();
		GameObject player = PhotonNetwork.Instantiate ("Player", spawnManager.spawnPoint.position, spawnManager.spawnPoint.rotation,0);
		GameManager.instance.SetLocalPlayer(player);
		networkStatusText.enabled = false;
		GameManager.instance.uiManager.HUD ();
	}
	public void LeaveGame()
	{	
		PhotonNetwork.Destroy(GameManager.instance.localPlayer);
		PhotonNetwork.LeaveRoom();
		networkStatusText.enabled = true;
	}

}
